<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    vik.liu
 *
 * 代理结算
 * 2017-11-11
 */
namespace osc\common\service;
use think\Db;
class Sttl_pro{
	
	//推荐奖
	function sttl_share($num,$orderid)
	{
		//返点

		
		$uid=is_login();

		$username = session("username");
		$member =  Db::name('member')->where('username',$username)->find();
        $config = Db::name('config')->where('name','self_bonus')->find();

        $day = date('d');

        $u_state = 1;
        
        
        if($member['user_state']==0)
        {
        	$u_state = 0;
         	$ret = Db::name('member')->where('username',$member['username'])->update(['user_state'=>1]);       	
        }
        

        $share1user = Db::name('member')->where('username',$member['sharemobile'])->find();
        $share2user = Db::name('member')->where('username',$share1user['sharemobile'])->find();
        $share3user = Db::name('member')->where('username',$share2user['sharemobile'])->find();
        //$config = Db::name('config')->where('name','share1bonus')->find();

       
        //var_dump($config);
        //$share1bonus = $config['value'] * $price;


        //更新上一级用户的直推总销售额度
        $rate = 0.1;
		
        $ret = Db::name('member')->where('username',$share1user['username'])->setInc('predeposit',$price*0.1);
        $dt1['frist_user'] = $username;
				
        Db::name('member')->where('username',$member['sharemobile'])->update($dt1);

        $data_acc['username']=$share1user['username'];
        $data_acc['num']=$num*0.1;  
        $data_acc['creat_time']=time();
        $data_acc['description']='分享合约';
        $ret = Db::name('stock')->insert($data_acc);
				

        if($share1user['frist_user'] == $username)
        {
        	$ret = Db::name('member')->where('username',$share1user['username'])->setInc('predeposit',$price*0.1);
        	$data_acc['username']=$share1user['username'];
	        $data_acc['num']=$price*0.1;  
	        $data_acc['creat_time']=time();
	        $data_acc['description']='分享奖励:用户'.$username.'消费'.$price.' 获得奖励'.$price*0.1;
	        $ret = Db::name('account')->insert($data_acc);  
        }else
        {
        	$rate = 0.2;
	        $ret = Db::name('member')->where('username',$share1user['username'])->setInc('predeposit',$price*0.2);

	        $data_acc['username']=$share1user['username'];
	        $data_acc['num']=$price*0.2;  
	        $data_acc['creat_time']=time();
	        $data_acc['description']='分享奖励:用户'.$username.'消费'.$price.' 获得奖励'.$price*0.2;
	        		$ret = Db::name('account')->insert($data_acc); 
        }

        		

        	}


        }
        
		
        //如果用户是第一次购买，需要给上级用户增加推荐人数
        //获取更新前的推荐数
        $befor_share_count=$share1user['share_count'];
        if(0==$member['is_consume'])
        {

        	$ret = Db::name('member')->where('username',$share1user['username'])->setInc('share_count',1);
        	$ret = Db::name('member')->where('username',$username)->setInc('is_consume',1);

        }

        //获取上上级用户用户数据
        if($share2user['user_state']>=1)
        {
        	if($share2user['share_count']>=5)
        	{
        		$ret = Db::name('member')->where('username',$share2user['username'])->setInc('predeposit',$price*$rate*0.1);
        		//$data_pre['uid']=$share2user['uid'];
        		//$data_pre['username']=$share2user['username'];
        		//$data_pre['money']=$price*$rate*0.1;  
        		//$data_pre['prefix']=1;
        		//$data_pre['description']='获得直接推荐用户'.$share1user['username'].'的管理奖励'.$price*$rate*0.1;
        		//$ret = Db::name('predeposit')->insert($data_pre);


        		$data_acc['username']=$share2user['username'];
        		$data_acc['num']=$price*$rate*0.1;  
        		$data_acc['creat_time']=time();
        		$data_acc['description']='管理奖励:用户'.$username.'消费'.$price.' 获得奖励'.$price*$rate*0.1;
        		$ret = Db::name('account')->insert($data_acc);
        	}
        }

        //
        if($share3user['user_state']>=1)
        {
        	if($share3user['share_count']>=8)
        	{
        		$ret = Db::name('member')->where('username',$share3user['username'])->setInc('predeposit',$price*$rate*0.1);
        		//$data_pre['uid']=$share3user['uid'];
        		//$data_pre['username']=$share3user['username'];
        		//$data_pre['money']=$price*$rate*0.1;  
        		//$data_pre['prefix']=1;
        		//$data_pre['description']='获得间接推荐用户'.$share1user['username'].'的管理奖励'.$price*$rate*0.1;
        		//$ret = Db::name('predeposit')->insert($data_pre);

        		$data_acc['username']=$share3user['username'];
        		$data_acc['num']=$price*$rate*0.1;  
        		$data_acc['creat_time']=time();
        		$data_acc['description']='管理奖励:用户'.$username.'消费'.$price.' 获得奖励'.$price*$rate*0.1;
        		$ret = Db::name('account')->insert($data_acc);

        	}
        }

       
        

		//给代理结算费用
		//$agent
		$shareagent = $this->getshareagent($member['sharemobile']);
		$agent = $this->getagent($username);
		//var_dump($member['sharemobile']);
		//var_dump($agent);
		//die;
		
		//结算用户推荐关系的代理人150
		if($shareagent)
		{

			$ret = Db::name('member')->where('username',$shareagent['username'])->setInc('predeposit',$price*0.05);
			    //$data_agent['uid']=$shareagent['uid'];
        		//$data_agent['username']=$shareagent['username'];
        		//$data_agent['money']=100;  
        		//$data_agent['prefix']=1;
        		//$data_agent['description']='代理奖励，团队用户'.$username.'消费'.$price;

        		$data_acc['username']=$shareagent['username'];
        		$data_acc['num']=$price*0.05;  
        		$data_acc['creat_time']=time();
        		$data_acc['description']='团队奖励，'.$username.'消费'.$price.' 获得奖励'.$price*0.05;
        		//更新代理的奖励
        		Db::name('agent')->where('username',$shareagent['username'])->setInc('total_bonus',$price*0.05);

        		//更新代理的会员数
        		if($u_state==0)
        		{
        			Db::name('agent')->where('username',$shareagent['username'])->setInc('member_num',1);
        		}
        		$ret = Db::name('agent_account')->insert($data_acc);
		}

		//给归属代理结算费用50
		if($agent)
		{
			$ret = Db::name('member')->where('username',$agent['username'])->setInc('predeposit',$price/60);
			//$data_agent1['uid']=$agent['uid'];
        	//$data_agent1['username']=$agent['username'];
        	//$data_agent1['money']=100;  
        	//$data_agent1['prefix']=1;
        	//$data_agent1['description']='代理奖励，归属用户'.$username.'消费'.$price;

        	$data_acc['username']=$agent['username'];
        	$bonus = $price/60;
        	$data_acc['num']=$price/60;  
        	$data_acc['creat_time']=time();
        	$data_acc['description']='区域奖励，用户'.$username.'消费'.$price.' 获得奖励'.($price/60).'金豆';
        	$ret = Db::name('agent_account')->insert($data_acc);
        	//die;
		}

		//更新代理的会员数量

		
		
		//获取代理的代理		
		$user = Db::name('member')->where('username',$shareagent['username'])->find();

		$shareagent1 = $this->getshareagent($user['sharemobile']);

		if($shareagent1)
		{

			$agent = Db::name('agent')->where('username',$shareagent1['username'])->find();
			//var_dump($agent);
			if($agent['state']==1)
			{
				//die;
				$ret = Db::name('member')->where('username',$shareagent1['username'])->setInc('predeposit',$price/37.5);
				$data_acc['username']=$shareagent1['username'];
	        	$data_acc['num']=$price/37.5;  
	        	$data_acc['creat_time']=time();
	        	$data_acc['description']='代理奖励，代理'.$shareagent['username'].'的用户'.$username.'消费'.$price.' 获得奖励'.$data_acc['num'].'金豆';

			}


			if($agent['state']==0)
			{
				$ret = Db::name('member')->where('username',$shareagent1['username'])->setInc('predeposit',$price/150);
				Db::name('agent')->where('username',$shareagent1['username'])->setInc('locking',$price/50);
				$data_acc['username']=$shareagent1['username'];
	        	$data_acc['num']=$price/37.5;  
	        	$data_acc['creat_time']=time();
	        	$data_acc['description']='代理奖励，代理'.$shareagent['username'].'的用户'.$username.'消费'.$price.' 获得奖励'.$data_acc['num'].'金豆,其中'.$data_acc['num'].'金豆冻结';
			}
		    $ret = Db::name('agent_account')->insert($data_acc);
		}

		
			
		

		//给代理的代理结算费用20
		//

	}


	function getshareagent($username)
	{
		if(!$username)
		{
			return null;
		}
		$user = Db::name('member')->where('username',$username)->find();
		if($user['is_agent']==1)
		{
			return $user;
		}else
		{
			return $this->getshareagent($user['sharemobile']);
		}


	}

	function getagent($username)
	{
		if(!$username)
		{
			return null;
		}
		$user = Db::name('member')->where('username',$username)->find();
		$agent = Db::name('agent')->where('id6',$user['id6'])->where(['state' => ['<',2]])->find();
		return $agent;

	}

	function team_bonus1($price,$orderid)
	{
        
	    $username = session("username");
		$member =  Db::name('member')->where('username',$username)->find();

		/*
		//查找商家
		$bsns_member=get_bsns('sharemobile');

		//设置奖金
		$config = Db::name('config')->find();
		$bsnsbonus = $config['bsns_bonus'] * $price;
        $ret = Db::name('member')->where('username',$bsns_member)->setInc('agent_pre_acc',$bsnsbonus);

        $date2['member']=$bsns_member;
        $data2['num']=$bsnsbonus;
        $data2['description'] = '代理得奖励(待分配账户)'.$bsnsbonus.',用户'.$username.'消费'.$price;
		$ret =  Db::name('agent_account')->insert($data2);	
        */


		

		//$bsmember =  Db::name('member')->where('username',$bsns_member)->find();

		$manage = $this->manage_member($member['username'],2);
		if(!$manage)
		{
			return;
		}

		if($manage['agent_level']=='3')
		{
			$config2 = Db::name('config')->where('name','bsns_bonus')->find();
			$manage3bonus = $config2['value'] * $price;
			$ret = Db::name('member')->where('username',$manage['username'])->setInc('agent_pre_acc',$manage3bonus);

			$data3['username']=$manage['username'];
        	$data3['num']=$manage3bonus;
        	$data3['creat_time'] = time();
        	$data3['order_id'] = $orderid;
        	$data3['description'] = '代理得奖励'.$manage3bonus.',消费'.$price;
			$ret =  Db::name('agent_account')->insert($data3);

			$manage = $this->manage_member($manage['username'],3);
			if(!$manage)
			{
				return;
			}

		}



		//给四星代理奖励
		if($manage['agent_level']=='4')
		{
			$config4 = Db::name('config')->where('name','manage1bonus')->find();
			$manage1bonus = $config4['value'] * $price;
			$ret = Db::name('member')->where('username',$manage['username'])->setInc('agent_pre_acc',$manage1bonus);

			$data4['username']=$manage['username'];
        	$data4['num']=$manage1bonus;
        	$data4['creat_time'] = time();
        	$data4['order_id'] = $orderid;
        	$data4['description'] = '代理得奖励'.$manage1bonus.',消费'.$price;
			$ret =  Db::name('agent_account')->insert($data4);

			$manage = $this->manage_member($manage['username'],4);
			if(!$manage)
			{
				return;
			}

		}
		if($manage['agent_level']=='5')//给五星代理奖励
		{
			$config5 = Db::name('config')->where('name','manage2bonus')->find();
			$manage2bonus = $config5['value'] * $price;
			$ret = Db::name('member')->where('username',$manage['username'])->setInc('agent_pre_acc',$manage2bonus);

			$data5['username']=$manage['username'];
        	$data5['num']=$manage2bonus;
        	$data5['creat_time'] = time();
        	$data5['order_id'] = $orderid;
        	$data5['description'] = '代理得奖励'.$manage2bonus.',消费'.$price;
			$ret =  Db::name('agent_account')->insert($data5);
		}
		



	}

	function manage_member($member,$level)
	{
		$user =  Db::name('member')->where('username',$member)->find();
		if(!$user)
		{
			return null;
		}
		if($user['agent_level']>$level)
		{
			return $user;
		}else
		{
			return $this->manage_member($user['sharemobile'],$level);
		}

	}

	function get_bsns($share)
	{
		$member =  Db::name('member')->where('username',$share)->find();
		if(!$member)
		{
			return null;
		}
		if($member['agent_level']>3)
		{
            return $member['username'];
		}else
		{
			return $this->get_bsns($member['sharemobile']);
		}

	}

 

}